package keepsoft.lcw.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.*;

import javax.sql.DataSource;

import keepsoft.lcw.dao.IntegratingDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;


public class IntegratingDaoImpl implements IntegratingDao {

    public static final Logger logger = LoggerFactory.getLogger(IntegratingDaoImpl.class);

    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    /**
     * 模拟批量插入数据
     *
     */
    @Override
    public void insertData() {
        String sql = "INSERT INTO gr_monitor_data (insert_time,fsjcd,create_time,type,name,flow) VALUES (?,?,?,?,?,?)";
        ArrayList monitors=new ArrayList<>();
        ArrayList monitorsName=new ArrayList<>();
        ArrayList types=new ArrayList();
        List li=findMonitors();
        for(int i=0;i<li.size();i++){
            Map<String, Object> tempMap=(Map<String,Object>)li.get(i);
            for (Iterator j = tempMap.keySet().iterator(); j.hasNext();) {
                Object obj = j.next();
                if(obj.equals("number")){
                    monitors.add(tempMap.get(obj));
                }else if(obj.equals("name")){
                    monitorsName.add(tempMap.get(obj));
                }else if(obj.equals("type")){
                    types.add(tempMap.get(obj));
                }
            }
        }
        List<Date> list = getTimeSegment();
        for (int i = 0; i < monitors.size(); i++) {
            for(Date date : list){
                float x=(float)(Math.random()*6);
                jdbcTemplate.update(sql, new Object[]{date,monitors.get(i),new Date(),types.get(i),monitorsName.get(i),x});
            }
        }
    }

    @Override
    public List findMonitors() {
        String sql="select * from monitor_base";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        return list;
    }

    public static List<Date> getTimeSegment() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month=cal.get(Calendar.MONTH)+1;
        int day=cal.get(Calendar.DAY_OF_MONTH);

        cal.set(year, month - 1, day, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long startTime = cal.getTimeInMillis();
        cal.set(year, month - 1, day, 23, 59, 59);
        long endTime = cal.getTimeInMillis();
        final int seg = 60 * 60 * 1000;//五分钟
        ArrayList<Date> result = new ArrayList<Date>((int) ((endTime - startTime) / seg + 1));
        for (long time = startTime; time <= endTime; time += seg) {
            result.add(new Date(time));
        }
        return result;
    }

    public static void main(String[] args) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Date> list = getTimeSegment();
        for(Date date : list){
            System.out.println(fmt.format(date));
        }
        System.out.println(list.size());
    }
}
